package com.sinovatech.ologger.util;

import java.awt.Font;
import java.awt.font.FontRenderContext;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 2004-9-21 Time: 17:30:02
 */
public class FixWidthString {
	static Font font = Font.getFont("sun.java2d.fontpath");

	static FontRenderContext frc = new FontRenderContext(null, false, false);

	public static String keep(String longString, int maxPoint) {
		if (longString != null) {
			char[] cr = longString.toCharArray();
			int index = 0, w = 0;
			if (font == null) {
				font = new Font("Monospaced", Font.PLAIN, 12);
			}
			for (index = 0; index < cr.length && w <= maxPoint; index++) {
				w = (int) font.getStringBounds(cr, 0, index + 1, frc).getWidth();
			}
			if (index < cr.length)
				return String.copyValueOf(cr, 0, Math.max(index - 1, 0)) + "...";
			else
				return longString;
		} else
			return "";
	}
}
